import { ipcRenderer } from 'electron'
import { contextBridge } from 'electron/renderer'

// import { electronAPI } from '@electron-toolkit/preload'
// contextBridge.exposeInMainWorld('electron', electronAPI)

contextBridge.exposeInMainWorld('SDK', {
  send,
  receive
})

contextBridge.exposeInMainWorld('electron', {
  getVersion: () => ipcRenderer.invoke('get-version'),
  getMacAddress: () => ipcRenderer.invoke('get-mac-address'),
  windowAllClosed: () => ipcRenderer.invoke('window-all-closed'),
  onUpdateavailable: (callback) => ipcRenderer.on('update-available', callback),
  startupdate: (text) => { ipcRenderer.send('startupdatesends', text) },
  onUpdate: (callback) => ipcRenderer.on('update', callback),
  onDownloaded: (callback) => ipcRenderer.on('downloaded', callback),
  installsend: (text) => { ipcRenderer.send('installsends', text) },
  getPackage: () => ipcRenderer.invoke('get-package'),
  
  installshortcut: (text) => { ipcRenderer.send('installshortcut', text) },
  useShortcut: (callback) => ipcRenderer.on('use-shortcut', callback),

  openWebview: (text) => ipcRenderer.invoke('open-webview', text),
  getchromiums: (text) => ipcRenderer.invoke('get-chromiums', text),

  downloadBrowser: (text) => { ipcRenderer.send('download-browser', text) },
  onDownloadBrowser: (callback) => ipcRenderer.on('on-download-browser', callback),
  closeBrowser: (callback) => ipcRenderer.on('close-browser', callback),
})

function send(type, data) {
  ipcRenderer.send(type, data)
}

function receive(channel, func) {
  ipcRenderer.on(channel, (event, ...args) => func(...args));
}

